/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package test.suite.tkaframework.support.behavior
{

import flash.events.Event;
import flash.events.IEventDispatcher;

import mx.core.UIComponent;
import mx.events.PropertyChangeEvent;

import org.teotigraphix.dx.interfaces.IComponentBehavior;
import org.teotigraphix.dx.interfaces.IComponentModel;
import org.teotigraphix.tka.patterns.behavior.SparkComponentBehavior;

import spark.primitives.SimpleText;

import test.suite.tkaframework.support.model.ITestComponentModel;

[ComponentHandler(event="myComponentPropertyChanged", handler="componentHandler")]

[SkinHandler(event="mySkinPropertyChanged", handler="skinHandler")]

[ModelHandler(event="propertyChange", handler="modelPropertyChangeHandler")]
[ModelHandler(event="testModelPropertyChanged", handler="testModelPropertyChangeHandler")]



public class TestBehavior extends SparkComponentBehavior
{
	public var componentFlag:Boolean = false;
	public var skinFlag:Boolean = false;
	public var skinPartFlag:Boolean = false;
	public var modelFlag:Boolean = false;
	
	public var activateFlag:Boolean = false;
	public var deactivateFlag:Boolean = false;
	
	public var mySkinPartAddedFlag:Boolean = false;
	public var mySkinPartRemovedFlag:Boolean = false;
	
	public var behaviorAddedSelfFlag:Boolean = false;
	public var behaviorRemovedSelfFlag:Boolean = false;	
	
	public var modelAddedFlag:Boolean = false;
	public var modelRemovedFlag:Boolean = false;		
	
	[ComponentAlias]
	public var myComponentAlias:UIComponent;
	
	[ModelAlias]
	public var myModelAlias:ITestComponentModel;		
	
	[ModelAlias]
	public var myPreModelAlias:ITestComponentModel;	
	

	
	[SkinPartAlias]
	[SkinPartHandler(event="mySkinPartPropertyChanged", handler="skinPartHandler")]
	public var mySkinPart:SimpleText;	
	
	[StyleBinding]
	public var myStyleBinding:String = "originalBindingValue";
	
	public function TestBehavior()
	{
		super();
	}
	
	override public function activate(host:IEventDispatcher):void
	{
		activateFlag = true;
	}
	
	override public function deactivate(host:IEventDispatcher):void
	{
		deactivateFlag = true;
	}	
	
	override protected function partAdded(partName:String, instance:Object):void
	{
		super.partAdded(partName, instance);
		
		if (instance == mySkinPart)
		{
			mySkinPartAddedFlag = true;
		}
	}
	
	override protected function partRemoved(partName:String, instance:Object):void
	{
		super.partRemoved(partName, instance);
		
		if (instance == mySkinPart)
		{
			mySkinPartRemovedFlag = true;
		}		
	}
	
	override protected function behaviorAdded(api:Class, behavior:IComponentBehavior):void
	{
		if (behavior === this)
		{
			behaviorAddedSelfFlag = true;
		}
	}
	
	override protected function behaviorRemoved(api:Class, behavior:IComponentBehavior):void
	{
		if (behavior === this)
		{
			behaviorRemovedSelfFlag = true;
		}		
	}
	
	override protected function modelAdded(api:Class, model:IComponentModel):void
	{
		modelAddedFlag = true;
	}
	
	override protected function modelRemoved(api:Class, model:IComponentModel):void
	{
		modelRemovedFlag = true;	
	}		
	
	component_handler function componentHandler(event:Event):void
	{
		//trace("TestBehavior.componentHandler()");
		componentFlag = !componentFlag;
	}
	
	skin_handler function skinHandler(event:Event):void
	{
		//trace("TestBehavior.skinHandler()");
		skinFlag = !skinFlag;
	}	
	
	skinpart_handler function skinPartHandler(event:Event):void
	{
		//trace("TestBehavior.skinPartHandler()");
		skinPartFlag = !skinPartFlag;
	}
	
	public var newModelProperty:String;
	
	model_handler function modelPropertyChangeHandler(event:PropertyChangeEvent):void
	{
		if (event.property.toString() == "modelProperty")
		{
			modelFlag = !modelFlag;
			newModelProperty = event.newValue as String;
		}
	}
	
	model_handler function testModelPropertyChangeHandler(event:Event):void
	{
		modelFlag = !modelFlag;
		newModelProperty = myModelAlias.testModelProperty;
	}	
	
}
}